﻿using System.Collections.ObjectModel;
using System.Threading;
using TheLocal.Common;
using TheLocal.Core;
using TheLocal.Model;

namespace TheLocal.ViewModel
{
    public class NewsListViewModel : ObservableCollection<NewsItem>
    {
        private NewsItem selectedNewsItems;
        private SynchronizationContext mainContext;

        public NewsListViewModel()
        {

        }

        public void LoadNewsContent(NewsItem selectedNewsItem, Country selectedCountry)
        {
            this.selectedNewsItems = selectedNewsItem;

            Downloader downloader = new Downloader(LoadNewsContentCallback);
            downloader.Download(string.Format("{0}{1}{2}", Constants.WebsiteHost, selectedCountry.Code, selectedNewsItem.ArticlePath));
            this.mainContext = SynchronizationContext.Current;
        }

        public void LoadNewsContentCallback(string html)
        {
            SynchronizationContextExtentions.InvokeIfRequired(this.mainContext, () =>
                {
                    Parser parser = new Parser();

                    selectedNewsItems.Content = parser.GetNewsContentFromHtml(html);
                });
        }
    }
}
